<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_session.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_inside_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_seats.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
$title = 'Choose Seat';

$ticket = $_SESSION['book_info'];

if (!empty($_SESSION['book_info']['booking_id'])) {
    $lines = api_booking::get_all_booking_lines_by_booking_id($_SESSION['book_info']['booking_id'], 
            $_SESSION['book_info']['user_id']);
    foreach ($lines as $item) {
        $seat = api_seats::get_seat($_SESSION['book_info']['ticket_id'], $item['row_pos'], $item['column_pos']);
        api_seats::unchoose_seat($seat['id']);
    }
    api_booking::delete_booking_line_by_booking_id($_SESSION['book_info']['booking_id']);
    api_booking::delete_booking($_SESSION['book_info']['booking_id']);
}

$booking_id = api_booking::save_booking(0, $ticket['user_id'], $ticket['ticket_id'], $ticket['ticket_quantity'], 0, 0, 0, 0);
$_SESSION['book_info']['booking_id'] = $booking_id;

?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        ?>
        <link rel="stylesheet" href="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_CSS . 'ticket.css' ?>">
        <script type="text/javascript">
            var book_quantity = '<?php echo $ticket['ticket_quantity']; ?>';
            var booking_id = '<?php echo $booking_id; ?>';
            
            $(document).ready(function() {
                $("#btn_next").on("click", function() {
                    if (book_quantity === 0) {
                        window.location.href = '<?php echo LINK_ROOT; ?>/choose_combo.php';
                    } else {
                        alert("Remaining "+book_quantity+" ticket. Please book.");
                    }
                });
            });
        </script>
    </head>
    <body>
        <div class="ts-body-container">
            <div class="ts-wrapper">
                <div class="padding-top-large row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php';
                    
                    $rows = api_seats::get_all_rows($ticket['ticket_id']);
                    $columns = api_seats::get_all_columns($ticket['ticket_id']);
                    ?>
                </div>
                <input type="hidden" name="hidd_temp_ticket_id" value="<?php echo $ticket['ticket_id']; ?>"/>
                <input type="hidden" name="hidd_temp_ticket_quantity" value="<?php echo $ticket['ticket_quantity']; ?>"/>
                <div class="ui grid">
                    <div class="row">
                        <div class="column">
                            <!-- Step -->
                            <div id='booking_step' class="ui four steps margin-top-normal">
                                <div class="ui disabled step">
                                    Choose Cinema & Time Start
                                </div>
                                <div class="ui active step">
                                    Choose Seats
                                </div>
                                <div class="ui disabled step">
                                    Choose Combo
                                </div>
                                <div class="ui disabled step">
                                    Coupon Code & Finish
                                </div>
                            </div>
                            <!-- End Step -->

                            <h1>Choose Seats</h1>
                            <div class="ui orange segment">
                                <div>
                                    <div align="center" id="divLegend">
                                        <?php
                                        $list = api_seats::get_all_seat_statuses();
                                        foreach ($list as $item) {
                                        ?>
                                        <span>
                                            <img border="0" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_TICKET . $item['image'] ?>" class="ImageAppletExample" id="imgAutoReserved">
                                            &nbsp;
                                            <span class="Legend" id="lblAutoReserved"><?php echo $item['name']; ?></span>
                                        </span>
                                        <?php
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                            <form id="form_seat" action="" method="post" name="form_seat">
                                <div class="ui orange segment">
                                    <input type="hidden" value="" id="hidden01" name="hidden01">
                                    <input type="hidden" value="" id="hidden02" name="hidden02">
                                    <div>
                                        <div style="width:560px;" class="Seating-Control" id="objSeatPlan">
                                            <div style="height: 363px;">
                                                <div style="margin-bottom: 40px; width: 315px; margin-left: 144px;" class="Seating-Screen">
                                                    <img style="width:87.500%;position:absolute;left:9.45px;" src="shared/layout/images/ticket/SeatScreen.png">
                                                </div>
                                                <div style="width: 300px; height: 290px; visibility: visible;" class="Seating-Container">
                                                    <div class="Seating-RowLabelContainer"><table cellspacing="0" cellpadding="0" style="position:absolute;top:2.9851%;height:97.0149%;">
                                                            <tbody>
                                                                <?php
                                                                $list = $rows;
                                                                foreach ($list as $item) {
                                                                ?>
                                                                <tr>
                                                                    <td style="background-color:white;"><?php echo $item['row_pos']; ?></td>
                                                                </tr>
                                                                <?php
                                                                }
                                                                ?>
                                                                
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                    <div data-originalsize="335" style="width:270px;height:290px;" class="Seating-Theatre">
                                                        <table cellspacing="0" cellpadding="0" style="left:0%;top:2.9851%;width:100%;height:97.0149%;table-layout:fixed;" data-area-number="1" class="Seating-Area" id="objSeatPlan_1">
                                                            <tbody>
                                                                <?php 
                                                                foreach ($rows as $row_index => $row) { ?>
                                                                    <tr style="line-height: 25px; height: 25px;">
                                                                        <td style="width:24.9803px;"></td>
                                                                    <?php
                                                                    foreach ($columns as $column_index => $column) { 
                                                                        $item = api_seats::get_seat($ticket['ticket_id'], $row['row_pos'], $column['column_pos']);
                                                                        ?>
                                                                        <td class="seat"><p style="width: 25px; line-height: 25px;"><?php echo $column['column_pos']; ?></p>
                                                                            <img data-col="<?php echo $column['column_pos']; ?>" 
                                                                                 data-row="<?php echo $row['row_pos']; ?>" 
                                                                                 <?php 
                                                                                 $image = $item['seat_status_image'];
                                                                                 if ($item['seat_status_id'] == 1) { // your seats
                                                                                    if (!api_booking::is_your_seat_available($booking_id, $row['row_pos'], $column['column_pos'])) {
                                                                                        $sold = api_seats::get_seat_status_by_status_id(4);
                                                                                        $image = $sold['image'];
                                                                                    }
                                                                                 }
                                                                                 ?>
                                                                                 src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_TICKET . $image; ?>" 
                                                                                 style="width: 25px; height: 25px;">
                                                                        </td>
                                                                    <?php
                                                                    }
                                                                    ?>
                                                                    </tr>
                                                                <?php
                                                                }
                                                                ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                    <div class="Seating-RowLabelContainer">
                                                        <table cellspacing="0" cellpadding="0" style="position:absolute;top:2.9851%;height:97.0149%;">
                                                            <tbody>
                                                                <?php
                                                                $list = $rows;
                                                                foreach ($list as $item) {
                                                                ?>
                                                                <tr>
                                                                    <td style="background-color:white;"><?php echo $item['row_pos']; ?></td>
                                                                </tr>
                                                                <?php
                                                                }
                                                                ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>
                                            <table cellspacing="0" cellpadding="0" class="Seating-Popup">
                                                <tbody>
                                                    <tr>
                                                        <td class="Seating-PopupMessage"></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                            <input type="hidden" id="objSeatPlan_SelectedSeatsHiddenField" name="objSeatPlan:SelectedSeatsHiddenField">
                                        </div>
                                    </div>


                                </div>
                                <div class="ui orange segment">
                                    <div class="ts-center">
                                        <div class="ui buttons">
                                            <!--<div class="ui green button">Back</div>-->
                                            <a href="tickets_quantity.php#booking_step" class="ui purple button">Back</a>
                                            <div class="or"></div>
                                            <!--<div class="ui orange button">Next</div>-->
                                            <div id="btn_next" class="ui orange button">Next</div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
                    ?>
                </div>
            </div>
        </div>
    </body>
</html>